﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace BindingLesson.Example
{
    /// <summary>
    /// ColumnChartWindow.xaml 的交互逻辑
    /// </summary>
    public partial class ColumnChartWindow : Window
    {
        ChartDataSource dataSource = new ChartDataSource();
        public ColumnChartWindow()
        {
            InitializeComponent();
            this.DataContext = dataSource;

            
        }

        public class ChartDataSource
        {
            public ObservableCollection<DataItem> Items { get; set; } = new ObservableCollection<DataItem>();

            public ChartDataSource()
            {
                for (int i = 0; i < 10; i++)
                {
                    Items.Add(new DataItem() { Name = "Item" + i, Value = new Random().Next(1, 30) });
                }

                Task.Run(async () =>
                {
                    while (true)
                    {
                        // Simulate data loading
                        await Task.Delay(1000); // Use Task.Delay instead of Thread.Sleep to avoid blocking the thread
                        Application.Current.Dispatcher.Invoke(() =>
                        {
                            Items.Add(new DataItem() { Name = "Item" + Items.Count, Value = new Random().Next(1, 30) });
                        });
                    }
                });
            }

        }


        public class DataItem
        {
            public string Name { get; set; }
            public int Value { get; set; }
        }
    }
}
